package UI.todos;

import gestorBD.Controlador;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import uiMain.OpcionDeMenu;
import static uiMain.bienRaizOk.ventana;


public class PanelOpcionCambiarContrasenha extends OpcionDeMenu {

    JLabel labelTitulo;
    JLabel labelContrasenhaActual;
    JLabel labelNuevaContrasenha;
    JLabel labelConfirmarNuevaContrasenha;

    JPasswordField campoContrasenhaActual;
    JPasswordField campoContrasenhaNueva;
    JPasswordField campoConfirmarNuevaContrasenha;

    JButton botonAceptar;
    JButton botonVolver;

    public PanelOpcionCambiarContrasenha() {
        super();
    }

    @Override
    public void ejecutar() {
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        labelTitulo = new JLabel("Ingrese la siguiente información");
        c.fill = 1;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(labelTitulo, c);

        labelContrasenhaActual = new JLabel("Contraseña actual:");
        c.fill = 1;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(labelContrasenhaActual, c);

        campoContrasenhaActual = new JPasswordField();
        campoContrasenhaActual.setColumns(10);

        c.fill = 1;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(campoContrasenhaActual, c);

        labelNuevaContrasenha = new JLabel("Contraseña nueva:");
        c.fill = 1;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(labelNuevaContrasenha, c);

        campoContrasenhaNueva = new JPasswordField();
        c.fill = 1;
        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(campoContrasenhaNueva, c);

        labelConfirmarNuevaContrasenha = new JLabel("Confirme contraseña nueva:");
        c.fill = 1;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(labelConfirmarNuevaContrasenha, c);

        campoConfirmarNuevaContrasenha = new JPasswordField();
        c.fill = 1;
        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(campoConfirmarNuevaContrasenha, c);

        botonAceptar = new JButton("Aceptar");
        c.fill = 1;
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(botonAceptar, c);

        botonVolver = new JButton("Cancelar");
        c.fill = 1;
        c.gridx = 1;
        c.gridy = 4;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(botonVolver, c);

        ventana.mostrarPanel(this);
        campoContrasenhaActual.requestFocusInWindow();
        //Se añaden las funcionalidades
        botonAceptar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                String contrasenhaActual = String.valueOf(campoContrasenhaActual.getPassword());
                String contrasenhaNueva = String.valueOf(campoContrasenhaNueva.getPassword());
                String confirmacionNueva = String.valueOf(campoConfirmarNuevaContrasenha.getPassword());

                if (campoContrasenhaActual.getPassword().length == 0 || campoContrasenhaNueva.getPassword().length == 0 || campoConfirmarNuevaContrasenha.getPassword().length == 0) {
                    String textoPopUp = "Por favor complete todos los campos";
                    JOptionPane.showMessageDialog(null, textoPopUp, "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (contrasenhaNueva.equals(confirmacionNueva)) {

                    boolean sePudoCambiar = Controlador.cambiarContrasenha(contrasenhaActual, contrasenhaNueva);

                    if (sePudoCambiar) {
                        String textoPopUp = "Su contraseña ha sido cambiada exitosamente";
                        JOptionPane.showMessageDialog(null, textoPopUp, "Contraseña cambiada", JOptionPane.INFORMATION_MESSAGE);
                        ventana.mostrarPanelAnterior();
                    } else {

                    }
                } else {
                    String textoPopUp = "Las dos contraseñas nuevas no son iguales";
                    JOptionPane.showMessageDialog(null, textoPopUp, "Contraseña incorrecta", JOptionPane.ERROR_MESSAGE);
                    campoContrasenhaActual.setText("");
                    campoContrasenhaNueva.setText("");
                    campoConfirmarNuevaContrasenha.setText("");
                }
            }

        });

        botonVolver.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ventana.mostrarPanelAnterior();
            }

        });
    }

    @Override
    public String toString() {
        return "Cambiar mi contraseña";
    }

}
